package com.calvin.study.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.calvin.study.service.StorageService;

import cloud.alibaba.study.entity.base.ResponseVo;
import cloud.alibaba.study.entity.storage.Storage;
import cloud.alibaba.study.utils.GsonUtil;

@Controller
@RequestMapping("storage")
public class StorageController {
	
	@Autowired
	private StorageService storageService;
	
	@RequestMapping("reduce")
	public @ResponseBody ResponseVo reduceInventory(@RequestBody String jsonStr) {
		try {
			if(StringUtils.isBlank(jsonStr)) {
				throw new Exception("json数据为空");
			}
			
			Storage storage = GsonUtil.fromJson(jsonStr, Storage.class, GsonUtil.DATE_FORMAT);
			if(null == storage) {
				throw new Exception("扣减库存参数不能为空");
			}
			
			storageService.reduceInventory(storage);
			return ResponseVo.success();
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseVo.failure("扣减库存失败：" + e.getMessage());
		}
	}

}
